package cn.lsoft.undoner.service.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cn.lsoft.undoner.dao.MessageDao;
import cn.lsoft.undoner.model.MessageRequest;
import cn.lsoft.undoner.service.ClientService;
import cn.lsoft.undoner.service.ScheduleService;

/**
 * @author <A HREF="mailto:undoner@gmail.com">undoner</A>
 * @date 11-25-2014
 */
@Service
public class ScheduleServiceImpl implements ScheduleService,ApplicationContextAware {
    private static final Logger LOGGER = Logger.getLogger(ScheduleServiceImpl.class);
    public static final String TIMEZONE_ID = "UTC";
    private ApplicationContext context;
    @Autowired
    private ClientService clientService;
    
    @Value("${mq.message.queue}")
    private String messageQueue;

    @Value("${mq.message.dead.queue}")
    private String messageDeadQueue;

    @Value("${mq.message.queue.alert.size:0}")
    private int messageQueueAlertSize;

    @Value("${mq.message.dead.queue.alert.size:0}")
    private int messageDeadQueueAlertSize;

    @Qualifier("messageDaoImpl")
    @Autowired
    private MessageDao messageDao;

    private Gson gson;

    private static Hashtable<String,Long> hsMqLimit = new Hashtable<String,Long>();

    @PostConstruct
    public void init() {
        gson = new GsonBuilder().create();
    }
    /**
     * 定时查询数据库中消息
     * @param
     * @return
     * @throws Exception
     */
    @Scheduled(cron = "${message.query.queue.cron:0 1 * * * ?}")
    @Override
    public void queryQueue()  throws Exception{
        List<MessageRequest> messageList = messageDao.queryMessageList();
        LOGGER.debug("queryQueue:" + gson.toJson(messageList));
    }
    
    /**
     * 定时查询dubbo接口
     * @param
     * @return
     * @throws Exception
     */
    @Scheduled(cron = "${message.query.queue.cron:0 1 * * * ?}")
    @Override
    public void queryDubboAPI() throws Exception{
//    	ClientService clientService = (ClientService)context.getBean("clientService"); // 获取远程服务代理
        String hello = clientService.sayHello("world"); // 执行远程方法
        List<String> nameList =new ArrayList<String>();
        nameList.add("undoner");
        nameList.add("yuanxiaojie");
        List<String> resultList = clientService.signInNames(nameList); // 执行远程方法
        LOGGER.debug( hello ); // 显示调用结果
        for(String str:resultList){
        	LOGGER.debug( str ); // 显示调用结果
        }
    }
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		context=arg0; 
	}

}
